clear all; close all; clc;
N = 1e6;
x = rand(1, N);
figure(1)
hist(x);
xlabel('x'); xlim([0 1]);
a = 10; b = 22; x = x*(b-a) + a;
dx = 0.01;
[h, xb] = hist(x, a-dx/2:dx:b+dx/2);
figure(2)
p = h / (sum(h) * dx);
plot(xb, p);
ylim([0 0.5]);
xlabel('x'); ylabel('p(x)');


M = 30; N = 1e6;
x_n = randn(1, N);
[h, x] = hist(x_n, 30);
inte = sum(h) * (x(2) - x(1)); p = h / inte;
figure(3); plot(x, p, 'r', 'LineWidth', 5);
hold on; bar(x, p); hold off
xlabel('X'); ylabel('P(X)');



figure(4)
x = sqrt(randn(1, N).^2 + randn(1, N).^2);
hist(x, 30)

figure(5)
x = sqrt((10+randn(1, N)).^2 + (10+randn(1, N)).^2);
hist(x, 30)

qcno_dB = 45;
N = 10000;
stdn = 8;
stdn_IQ = sqrt(stdn^2 * N / 2);

ro1 = 0.75;
ro2 = 0.5;
Dp = stdn_IQ^2; Dpe = ro1*stdn_IQ^2; Del = ro2*stdn_IQ^2;

L=chol([Dp Dpe Dpe;
        Dpe Dp Del;
        Dpe Del Dp])';
Nj = 100000;
nIp = nan(1, Nj);
nIe = nan(1, Nj);
nIl = nan(1, Nj);
for j = 1:Nj
     nI = L*randn(3,1);
     
     nIp(j)=nI(1);
     nIe(j)=nI(2);
     nIl(j)=nI(3);
     
end

fprintf('Corrcoeff nIp nIe = %f\n', mean(nIp.*nIe / std(nIp)/std(nIe)));

fprintf('Corrcoeff nIl nIe = %f\n', mean(nIl.*nIe / std(nIl)/std(nIe)));


